// TEST 01

#include <Ethernet.h>

// Ethernet stuff
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192,168,0,177 };
byte subnet[] = {
  255,255,255,0};
byte gateway[] = {
  192,168,0,1};
  
// Webserver
int websource = 0; // from where did the request come? 1=Stable, 2=Manual Run, 3=Test
Server server(80);

void setup(){
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
}

void loop(){
  webserver();
}

void webserver(){
  server.begin();
  Client client = server.available();
  if (client) {
    TextFinder  finder(client );  
    while (client.connected()) {      
      if (client.available()) { 
        boolean dodelay = false;
        boolean dostop = false;
        boolean dogo = false;        
        if( finder.find("GET /") ) {              
          while(finder.findUntil("command", "\n\r")){  
            char type = client.read();
            int syntax_1 = finder.getValue();
            switch (type) {
              
              // STOP-Case
              case 'S':
              Serial.println("STOP!");
              stopall();
              break;
              
              // Delay-Case
              case 'D':
              Serial.println("Delay!");
              prog_delay();
              break;
              
              // GO-Case (manueller Start)
              case 'G':
              Serial.println("GO!");
              switch (syntax_1){
                case 10:
                  Serial.print("Case 10");
                break;
                
                case 11:
                  Serial.print("Case 10");
                break;
                
              }
              break;
              
              default:
              Serial.println("Keine Eingabe erkannt!");
            }
          }                         
        }
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();
        client.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
        client.println("<html <html xmlns=\"http://www.w3.org/1999/xhtml\">");
        client.println("<head>");
        client.println("<meta content=\"yes\" name=\"apple-mobile-web-app-capable\" />");
        client.println("<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />");
        client.println("<meta content=\"minimum-scale=1.0, width=device-width, maximum-scale=0.6667, user-scalable=no\" name=\"viewport\" />");
        switch (websource){
          case 1:
          client.println("<meta http-equiv=\"refresh\" content=\"2; URL=http://control.techgarden.info/\">");
          break;
          
          case 2:
          client.println("<meta http-equiv=\"refresh\" content=\"2; URL=http://control.techgarden.info/index2.html\">");
          break;
          
          case 3:
          client.println("<meta http-equiv=\"refresh\" content=\"2; URL=http://test.techgarden.info/relays.html\">");
          break;
        }
        
        client.println("<link href=\"http://control.techgarden.info/css/style.css\" rel=\"stylesheet\" media=\"screen\" type=\"text/css\" />");
        client.println("<script src=\"http://control.techgarden.info/javascript/functions.js\" type=\"text/javascript\"></script>");
        client.println("<title>Befehl abgesetzt</title>");
        client.println("</head>");
        client.println("<body>");
        client.println("<div id=\"topbar\" class=\"transparent\">");
        client.println("<div id=\"leftnav\">");
        client.println("<a href=\"http://control.techgarden.info/\"><img alt=\"home\" src=\"http://control.techgarden.info/images/home.png\" /></a>");
        client.println("</div>");
        client.println("</div>");
        client.println("<div id=\"content\">");
        client.println("<span class=\"graytitle\">Eingabe erfolgreich</span>");
        client.println("<ul class=\"pageitem\">");
        client.print("<li class=\"textbox\"><span class=\"header\">Ihre Eingabe wurde bearbeitet</span><p>Durch diese Seite wird die Verarbeitung des Befehls quitiert.");
        client.println("Sie sollten autmoatisch weitergeleitet werden<br>Sollte dies nicht funktionieren - verwenden den Home Button</p></li></ul></ul>");
        client.println("<div id=\"footer\"><a href=\"http://iwebkit.net\">Powerd by iWebKit</a></div>");
        client.println("</body>");
        client.println("</html>");        
        break;        
      } 
    }
    delay(1);
    client.stop();
  }
}
